package cn.ronghuanet.auth.controller;

import cn.ronghuanet.auth.annotation.RonghuaPermission;
import cn.ronghuanet.auth.domain.Role;
import cn.ronghuanet.auth.dto.RoleDTO;
import cn.ronghuanet.auth.query.RoleQuery;
import cn.ronghuanet.auth.service.IRoleServiceI;
import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RonghuaPermission(name = "角色管理",descs = "角色管理api")
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleServiceI service;

    // 增
    @RonghuaPermission(name = "角色新增/修改",descs = "角色新增和修改是一个接口")
    @PostMapping
    public AjaxResult insert(@RequestBody Role role){
        try {
            if (Objects.nonNull(role)){
                if (Objects.nonNull(role.getId())){
                    service.update(role);
                }else{
                    service.insert(role);
                }
                return AjaxResult.success();
            }
            return AjaxResult.error("参数异常");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    // 删
    @RonghuaPermission(name = "删除指定角色")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @RonghuaPermission(name = "批量删除角色")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            service.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    // 查
    @RonghuaPermission(name = "查询指定角色")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id){
        try {
            return AjaxResult.success(service.selectById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @RonghuaPermission(name = "查询所有角色")
    @GetMapping
    public AjaxResult selectAll(){
        try {
            return AjaxResult.success(service.selectAll());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 分页查询的接口
    @RonghuaPermission(name = "角色分页查询")
    @PutMapping
    public AjaxResult queryPage(@RequestBody RoleQuery roleQuery){
        try {
            PageInfo<Role> pageInfo = service.queryPage(roleQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 对于前端传入参数 和 实体类不匹配的情况我们不是说改domain 就改
     * 方案一: 使用map
     * 方案二: 使用DTO接收 Data Transfer Object
     *        VO  View  Object你的返回给页面的数据 也不是domain的情况
     * @return
     */
    // 保存权限
    @RonghuaPermission(name = "授权新增/修改",descs = "授权新增和修改是一个接口")
    @PostMapping("/permission")
    public AjaxResult insertByRoleId(@RequestBody RoleDTO roleDTO){
        try {
            service.insertByRoleId(roleDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }
    // 根据角色id 查询对应的权限
    @RonghuaPermission(name = "根据角色id查询对应的权限")
    @GetMapping("/permission/{id}")
    public AjaxResult selectByRoleId(@PathVariable("id") Long id){
        try {
            List<String> sns=service.selectByRoleId(id);
            return AjaxResult.success(sns);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

}
